package lib::Storage;

# This file is part of noplagia.
#
# noplagia is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# noplagia is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# noplagia; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA  02111-1307 USA

use strict;
use Digest::SHA1 qw(sha1_hex);
use File::Basename;
use File::Copy;
use File::Path;
use File::Spec;
use lib::Context;

require Exporter;
our ($VERSION,@ISA,@EXPORT,$context);

$VERSION = 0.01;

=head1 NAME

    lib::Storage - Permet de stocker un fichier dans le depot de fichier

=head1 SYNOPSIS

    use lib::Storage;
    insert(path_src, username)


=head1 DESCRIPTION

    Ce module insere un fichier dans le depot de fichier et par default supprime la source.
    Ensuite il insere dans la base de donnees le path vers ce fichier au proprietaire
    de celui-ci.

=head1 FUNCTIONS

=over 2

=item  insert - insere un fichier dans le depot de fichier et marque $user comme le proprietaire dans
la bdd.

=cut

@ISA    = qw(Exporter);
@EXPORT = qw( &file_insert &handle_insert

);

# DEPRECATED
sub file_insert {
    my ($src, $user) = @_;
    my ($dst, $dir);

    die "Fichier '$src' inexistant " unless (-e $src);

    while (!$dst or -e $dst){
        $dir = sha1_hex($src.(time ^ $$));
        $dst = File::Spec->catfile( (substr($dir, 0, 3), substr($dir, 3, 3), ),  substr($dir, 6));
    }
    mkpath(dirname($dst)) or die "Erreur $!";
    eval {
        move($src, $dst) or die "Impossible de copier $src vers $dst -> $!";
    };
    if ($@) {
        rmdir(dirname($dst));
        rmdir(substr($dir, 0, 2));
        warn $@;
    }
    my $schema = $ENV{"NOPLAGIA_DB"};
    $schema->resultset('Users')->find({pseudo => $user})->add_to_files({nom => basename($src), path=>$dst});
    return $dst;

}



sub handle_insert {
    my ($file_handle, $filename, $user) = @_;
    my ($dst, $dir);

    die "Descripteur de fichier inexistant " unless (-e $file_handle);
    my $filepool_dir = lib::Context::config("filepool");
    while (!$dst or -e $dst){
        $dir = sha1_hex($filename.(time ^ $$));
        $dst = File::Spec->catfile( (substr($dir, 0, 3), substr($dir, 3, 3), ),  substr($dir, 6));
    }
    mkpath(dirname(File::Spec->catdir($filepool_dir,$dst))) or die "Erreur $!";
    eval {
        open(FILE, '>', File::Spec->catdir($filepool_dir,$dst));#read( INPUT, $var, -s INPUT );
        my $buffer;
        read($file_handle, $buffer, -s $file_handle);
        print FILE $buffer;
        close(FILE);
        close($file_handle); #fermeture du fichier temporaire
    };
    if ($@) {
        rmdir(File::Spec->catdir($filepool_dir,dirname($dst)));
        rmdir(File::Spec->catdir($filepool_dir,substr($dir, 0, 2)));
        warn $@;
    }
    lib::Context::set_conn_db( );
    my $schema = $ENV{'NOPLAGIA_DB'};
    $schema->resultset('Users')->find({pseudo => $user})->add_to_files({nom => basename($filename), path=>$dst});
    return $dst;
}


END { }
1;
__END__

=back

=head1 AUTEUR

    noplagia dev team

    http://noplagia.org

=cut



